package com.start;


import com.fx.view.ApplicationView;
import com.dto.config.IniDTO;
import com.sftpserver.SftpServerApplication;
import com.sftpserver.TestService;
import javafx.stage.StageStyle;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import com.fx.jfxsupport.AbstractJavaFxApplicationSupport;
import javafx.stage.Stage;
import org.springframework.context.annotation.ComponentScan;

import javax.annotation.PostConstruct;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 * @author jasonsang
 */


@SpringBootApplication/*启动控制*/
@Slf4j(topic = "启动类")/*日志*/
@ComponentScan({"com"})/*搜索路径*/
public class Main extends AbstractJavaFxApplicationSupport {
//    @Autowired
//    CncAlarm cncAlarm;



//    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
//    int width = gd.getDisplayMode().getWidth();
//    int height = gd.getDisplayMode().getHeight();

    /*获取计算机内核数*/
    private final static int AVALIABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
    /*新建线程池*/

    private final static ThreadPoolExecutor POOL_EXECUTOR = new ThreadPoolExecutor(AVALIABLE_PROCESSORS,
            AVALIABLE_PROCESSORS*2,
            1,
            TimeUnit.MINUTES,
            new LinkedBlockingDeque<>(5),
            new ThreadPoolExecutor.CallerRunsPolicy()
    );

    @Override
    public void init() throws Exception {

        super.init();
    }
    public static Class<?> main(String[] args) {

        //为了能正常使用Desktop.getDesktop()相关方法
        System.setProperty("java.awt.headless", Boolean.toString(false));


//        SpringApplication.run(Main.class, args);

        return launch(Main.class, ApplicationView.class, args);
    }

    /**
     * 静态方法定义
     */

    @PostConstruct
    public void startServer() throws Exception {
//        long start = System.currentTimeMillis();



//        /*开启异步单元执行任务*/
//        POOL_EXECUTOR.execute(() ->{
//            try {
//                /*启动opc服务*/
//                new ServerApplication();
//                doNotify("","已启动opcua服务","dialog-information.png");
//            } catch (Throwable throwable){
//                log.error("启动错误->",throwable);
//                doNotify("","opcua服务启动失败","dialog-warning.png");
//            }
//
//        });



        /*执行另一个任务*/
        POOL_EXECUTOR.execute(()->{
            try {

                //系统检测
                new TestService();
                /*启动sftp服务*/
                SftpServerApplication.start();

            } catch (Throwable throwable){
                log.error("启动错误->",throwable);

            }

        });


//        /*执行另一个任务*/
//        POOL_EXECUTOR.execute(()->{
//            try {
//                /*启动监听服务*/
//                cncAlarm.doFirst();
//            } catch (Throwable throwable){
//                log.error("启动监听服务错误->",throwable);
//            }
//
//        });


        /*执行另一个任务*/
//        POOL_EXECUTOR.execute(()->{
//            try {
//                /*启动监听服务*/
//                /*监听传输文件*/
//                PathUtil.listenFiles(Paths.get(IniDTO.getSftp_path()));
//
//            } catch (Throwable throwable){
//                log.error("监听传输文件错误->",throwable);
//            }
//
//        });


        /*同步等待线程结束*/
        /*挂起*/
//        Thread.currentThread().join();





    }

    /**
     * For this dummy app, the (JavaFX scenegraph) content, just says "hello, world".
     * A real app, might load an FXML or something like that.
     *
     * @return the main window application content.
     */

    @Override
    public void start(Stage stage) throws Exception {

        //隐藏窗体 外框
        stage.initStyle(StageStyle.TRANSPARENT);
        //scene.setFill(Color.TRANSPARENT);
        /*----读取pom版本号----*/
//        MavenXpp3Reader reader = new MavenXpp3Reader();
////        String myPom = System.getProperty("user.dir") + File.separator + "pom.xml";
//        String myPom =  PathUtil.getExePath()+ File.separator +"pom.xml";
//        Model model = reader.read(new FileReader(myPom));
//        System.out.println(model.getVersion());
        /*-----------------*/
        stage.setTitle("SFTP服务端");
       /*不可更改窗口大小*/
//        stage.setWidth(300);
//        stage.setHeight(600);
        stage.setResizable(false);
        super.start(stage);

    }




    @Bean
    public WebServerFactoryCustomizer webServerFactoryCustomizer(){

        return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() {
            @SneakyThrows
            @Override
            public void customize(ConfigurableServletWebServerFactory factory) {

                factory.setPort(Integer.parseInt(IniDTO.getHttpPort()));
            }
        };
    }


}
